//=============================================================================
// transform.fx by Frank Luna (C) 2004 All Rights Reserved.
//
// Basic FX that simply transforms geometry from local space to 
// homogeneous clip space, and draws the geometry in wireframe mode.
//=============================================================================

uniform extern float4x4		gWVP;
uniform extern float4x4		gWIT;
uniform extern float4x4		gWM;
uniform extern float3		eyePos, lightPos;
uniform extern float4		ambientLight, specularLight, diffuseLight;
uniform extern float3		lightAttenuation;
uniform extern texture		tex;
sampler						texs
{ 
	Texture = <tex>;
	// Tri-linear filtering 
	MinFilter = LINEAR; 
	MagFilter = LINEAR; 
	MipFilter = LINEAR;
};
	
// Define a vertex shader output structure; that is, a structure
// that defines the data we output from the vertex shader.  Here,
// we only output a 4D vector in homogeneous clip space.  The
// semantic ": POSITION0" tells Direct3D that the data returned
// in this data member is a vertex position.
struct OutputVS
{
    float4 posH  : POSITION0;
    float2 tex0	 : TEXCOORD0;
    float3 posW  : TEXCOORD1;
    float3 normW : TEXCOORD2;
};

// Define the vertex shader program.  The parameter posL 
// corresponds to a data member in the vertex structure.
// Specifically, it corresponds to the data member in the 
// vertex structure with usage D3DDECLUSAGE_POSITION and 
// index 0 (as specified by the vertex declaration).
OutputVS TransformVS(float3 posL : POSITION0, float2 texcoord: TEXCOORD0)
{
    // Zero out our output.
	OutputVS outVS = (OutputVS)0;
	
	// Transform to homogeneous clip space.
	outVS.posH = mul(float4(posL, 1.0f), gWVP);
	
	outVS.posW = mul(float4(posL, 1.0f), gWM);
	
	outVS.normW = mul(float4(posL, 1.0f), gWM);
	outVS.normW = normalize(outVS.normW);
	
	outVS.tex0 = texcoord;
	// Done--return the output.
    return outVS;
}

// Define the pixel shader program.  Just return a 4D color
// vector (i.e., first component red, second component green,
// third component blue, fourth component alpha).  Here we
// specify black to color the lines black. 
float4 TransformPS(OutputVS outVS) : COLOR
{
	float3 texColor = tex2D(texs, outVS.tex0).rgb;
	outVS.normW = normalize(outVS.normW);
	float3 toEye = normalize(eyePos - outVS.posH.xyz);
	float3 lightDir = normalize(lightPos - outVS.posW.xyz);
	//Time to calculate lighting!
	float3 R = dot(-lightDir, outVS.normW.xyz);
	float3 ambient = ambientLight;
	float3 diffuse = diffuseLight * dot(lightDir, outVS.normW.xyz);
	float3 specular = specularLight * saturate(dot(eyePos, R));
	
	float DistoLight = length(eyePos - outVS.posW.xyz);
	float Attenuation = (lightAttenuation.x + (DistoLight * lightAttenuation.y) + (DistoLight * DistoLight * lightAttenuation.z));
	
	return float4 (((ambient + (diffuse + specular)) * texColor)/ Attenuation, diffuseLight.a);
}

technique TransformTech
{
    pass P0
    {
        // Specify the vertex and pixel shader associated with this pass.
        vertexShader = compile vs_3_0 TransformVS();
        pixelShader  = compile ps_3_0 TransformPS();

		// Specify the render/device states associated with this pass.
		//FillMode = Wireframe;
    }
}